package gorminit

import (
	"log"
	"os"
	"time"
	gormLog "gorm.io/gorm/logger"
)

type gormLogger struct{}

func (*gormLogger) Printf(format string, v ...interface{}) {
	//fmt.Println(fmt.Sprintf(format, v...))
	/*count := len(v)
	_, ok := v[count-2].(string)
	if !ok{
		//写入到文件todo
		variable.ZapLog.Debug(
			"[gorm]",
			zap.Any("line", v[0]),
			zap.Any("time", v[count-3]),
			zap.Any("rows", v[count-2]),
			zap.Any("sql", v[count-1]),
		)
	}*/
}

func gormLogLevel(level int, slowThreshold int) gormLog.Interface {
	var writer gormLog.Writer
	var logLevel gormLog.LogLevel
	switch level {
	case 0: //输出到屏幕
		writer = log.New(os.Stdout, "\r\n", log.LstdFlags)
		logLevel = gormLog.Info
	case 1: //输出到文件
		writer = &gormLogger{}
		logLevel = gormLog.Info
	default: //什么都不输出
		writer = &gormLogger{}
		logLevel = gormLog.Silent
	}
	return setingLog(writer, logLevel, slowThreshold)
}

//配置日志
func setingLog(writer gormLog.Writer, level gormLog.LogLevel, slowThreshold int) gormLog.Interface {
	return gormLog.New(
		writer, // io writer
		gormLog.Config{
			SlowThreshold: time.Duration(slowThreshold) * time.Second,   // 慢 SQL 阈值
			LogLevel:      level, // Log level
			Colorful:      true,         // 彩色打印
		},
	)
}
